package org.chatboleh;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.OverlayLayout;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import org.apache.log4j.Logger;
import org.chatboleh.bean.Document;
import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class DocumentEditor extends JFrame {

	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(this.getClass());
	private Document document;
	private JTextField txtDocumentName;
	private JButton btnCreate;
	private JButton btnCreatePage;
	private TextElementProperties textElementProperties0;
	private JButton btnAddText;

	private int TEXT_ELEMENT_SIZE = 0;

	private JLabel dummyLabel;
	private JPanel jPanel0;
	private JLabel jLabel1;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";

	public DocumentEditor() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getTxtDocumentName(), new Constraints(new Leading(12, 204, 10, 10),
				new Leading(12, 12, 12)));
		add(getBtnCreate(), new Constraints(new Leading(228, 12, 12),
				new Leading(9, 12, 12)));
		add(getBtnCreatePage(), new Constraints(new Leading(369, 10, 10),
				new Leading(9, 40, 21)));
		add(getTextElementProperties0(), new Constraints(new Bilateral(12, 12,
				427), new Leading(324, 12, 12)));
		add(getBtnAddText(), new Constraints(new Leading(479, 12, 12),
				new Leading(9, 12, 12)));
		add(getDummyLabel(), new Constraints(new Leading(614, 10, 10),
				new Leading(318, 10, 10)));
		add(getJPanel0(), new Constraints(new Bilateral(12, 12, 0),
				new Leading(38, 270, 12, 12)));
		setSize(665, 460);
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("jLabel1");
		}
		return jLabel1;
	}

	private JPanel getJPanel0() {
		if (jPanel0 == null) {
			jPanel0 = new JPanel();
			jPanel0.setBorder(new LineBorder(Color.black, 1, false));
			jPanel0.setLayout(new GridLayout(2, 2));
		}
		return jPanel0;
	}

	private JLabel getDummyLabel() {
		if (dummyLabel == null) {
			dummyLabel = new JLabel();
			dummyLabel.setText("jLabel0");
			dummyLabel.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent event) {
					dummyLabelMouseMouseClicked(event);
				}
			});
			dummyLabel.addFocusListener(new FocusAdapter() {
				@Override
				public void focusLost(FocusEvent event) {
					dummyLabelFocusFocusLost(event);
				}
			});
		}
		return dummyLabel;
	}

	private JButton getBtnAddText() {
		if (btnAddText == null) {
			btnAddText = new JButton();
			btnAddText.setText("Add Text");
			btnAddText.setVisible(false);
			btnAddText.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent event) {
					btnAddTextMouseMouseClicked(event);
				}
			});
		}
		return btnAddText;
	}

	private TextElementProperties getTextElementProperties0() {
		if (textElementProperties0 == null) {
			textElementProperties0 = new TextElementProperties();
		}
		textElementProperties0.setVisible(false);
		return textElementProperties0;
	}

	private JButton getBtnCreatePage() {
		if (btnCreatePage == null) {
			btnCreatePage = new JButton();
			btnCreatePage.setText("Create Page");
			btnCreatePage.setVisible(false);
			btnCreatePage.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent event) {
					btnCreatePageMouseMouseClicked(event);
				}
			});
		}
		return btnCreatePage;
	}

	private JButton getBtnCreate() {
		if (btnCreate == null) {
			btnCreate = new JButton();
			btnCreate.setText("Create Document");
			btnCreate.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent event) {
					btnCreateMouseMouseClicked(event);
				}
			});
		}
		return btnCreate;
	}

	private JTextField getTxtDocumentName() {
		if (txtDocumentName == null) {
			txtDocumentName = new JTextField();
		}
		return txtDocumentName;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class. Note: This class is only created so that you can
	 * easily preview the result at runtime. It is not expected to be managed by
	 * the designer. You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				DocumentEditor frame = new DocumentEditor();
				frame.setDefaultCloseOperation(DocumentEditor.EXIT_ON_CLOSE);
				frame.setTitle("DocumentEditor");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void btnCreateMouseMouseClicked(MouseEvent event) {
		document = new Document();
		document.setName(txtDocumentName.getText());
		JOptionPane.showMessageDialog(this, "Document [" + document.getName()
				+ "] created.", "Create Document",
				JOptionPane.INFORMATION_MESSAGE);
		btnCreatePage.setVisible(true);
	}

	private void btnCreatePageMouseMouseClicked(MouseEvent event) {
		Graphics g = jPanel0.getGraphics();
		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(Color.white);
		g2d.fillRect(1, 1, 500, 250);
		btnAddText.setVisible(true);
		// jScrollPane0.paintComponents(g);
	}

	private OverlayLayout overlay;
	
	private void btnAddTextMouseMouseClicked(MouseEvent event) {
		textElementProperties0.setVisible(true);
		// final JLabel tempLabel = new JLabel();
		// tempLabel.setText("Text" + TEXT_ELEMENT_SIZE);
		// tempLabel.setSize(50, 12);
		// tempLabel.addMouseListener(new MouseAdapter() {
		// @Override
		// public void mouseClicked(MouseEvent event) {
		// // dummyMouseMouseClicked(event, tempLabel);
		// tempLabel.setBackground(Color.white);
		// }
		// });
		// dummyLabel = new JLabel();
		jPanel0 = new GridPanel();
		
		overlay = new OverlayLayout(jPanel0);
		jPanel0.setLayout(overlay);

//		SimpleMouseReporter reporter = new SimpleMouseReporter();
//		SimpleMouseMotionReporter mmr = new SimpleMouseMotionReporter();
		final JButton tempLabel = new JButton("Text" + TEXT_ELEMENT_SIZE++);
//		mmr.setChild(tempLabel);
//		tempLabel.addMouseListener(reporter);
		tempLabel.addMouseListener(new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
//				tempLabel.setBackground(Color.gray);
				System.out.println("mouseExited");
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				overlay.invalidateLayout(jPanel0);
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				jPanel0.doLayout();
			}
			
		});
//		tempLabel.addMouseMotionListener(mmr);
		tempLabel.addMouseMotionListener(new MouseMotionListener(){

			@Override
			public void mouseDragged(MouseEvent arg0) {
				// TODO Auto-generated method stub
				tempLabel.setAlignmentX(arg0.getX());
				tempLabel.setAlignmentY(arg0.getY());
			}

			@Override
			public void mouseMoved(MouseEvent arg0) {
				// TODO Auto-generated method stub
//				tempLabel.setBackground(Color.cyan);
				System.out.println("mouseExited");
			}
			
		});
		
		getContentPane().add(tempLabel, new Constraints(new Leading(5, 30, 10, 10), new Leading(12, 12, 12)));
		jPanel0.add(tempLabel);
		logger.debug(tempLabel.getAlignmentX());
		logger.debug(tempLabel.getAlignmentY());
		tempLabel.setAlignmentX(40);
		tempLabel.setAlignmentY(40);
		overlay.invalidateLayout(jPanel0);
		jPanel0.doLayout();

		// jPanel0.add(new JButton("Text" + TEXT_ELEMENT_SIZE++));
		// jPanel0.add(new JButton("Text" + TEXT_ELEMENT_SIZE++));
		// jPanel0.repaint();
		// this.add(dummyLabel, new Constraints(new Leading(5, 30, 10, 10),
		// new Leading(12, 12, 12)));
		// Graphics g = jScrollPane0.getGraphics();
		// Graphics2D g2d = (Graphics2D) g;
		// g2d.setColor(Color.black);
		// g2d.drawString("Text" + TEXT_ELEMENT_SIZE, 2, 10 );
		// TEXT_ELEMENT_SIZE++;
	}

	private void dummyLabelMouseMouseClicked(MouseEvent event) {
		dummyLabel.setBackground(Color.white);
	}

	private void dummyLabelFocusFocusLost(FocusEvent event) {
		dummyLabel.setBackground(Color.gray);
	}

	public class SimpleMouseMotionReporter implements MouseMotionListener {

		private JComponent child;

		public void setChild(JComponent comp) {
			this.child = comp;
		}

		Logger logger = Logger.getLogger(this.getClass());

		@Override
		public void mouseDragged(MouseEvent arg0) {
			// TODO Auto-generated method stub
			logger.debug(arg0.getX() + " : " + arg0.getY());
			child.setAlignmentX(Float.valueOf("" + arg0.getX()));
			child.setAlignmentY(Float.valueOf("" + arg0.getY()));
		}

		@Override
		public void mouseMoved(MouseEvent arg0) {
			// TODO Auto-generated method stub
			logger.debug(arg0.getX() + " : " + arg0.getY());
		}

	}

	public class SimpleMouseReporter implements ActionListener, MouseListener {
		Logger logger = Logger.getLogger(this.getClass());

		public void actionPerformed(ActionEvent ae) {
			logger.debug(ae.getActionCommand());
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			logger.debug("mouseClicked");
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			logger.debug("mouseClicked");
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			logger.debug("mouseExited");
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			logger.debug("mousePressed");
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			logger.debug("mouseReleased");
		}
	}

	public class GridPanel extends JPanel {
		public void paint(Graphics g) {
			super.paint(g);
			int w = getSize().width;
			int h = getSize().height;
			System.out.println("w: " + w + " h: " + h);
			g.setColor(Color.red);
			g.drawRect(0, 0, w - 1, h - 1);
			g.drawLine(w / 2, 0, w / 2, h);
			g.drawLine(0, h / 2, w, h / 2);
		}
	}

}
